package com.lemon.smartsafety.ui.adapter.main;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ldzs.recyclerlibrary.view.FullyGridLayoutManager;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.event.main.MainPanelButtonEvent;
import com.lemon.smartsafety.entity.main.MainPanelButton;
import com.lemon.smartsafety.entity.main.MainRcvItemBase;
import com.lemon.smartsafety.ui.adapter.common.RcvViewType;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.ViewUtils;

import java.util.List;

import cn.bingoogolapple.badgeview.BGABadgeFrameLayout;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/2/1.
 */
public class MainOptionItemViewRec implements AdapterItem<MainRcvItemBase<List<MainPanelButton>>> {
    private LoggerHelper loggerHelper = new LoggerHelper(MainOptionItemViewRec.class);

    private RecyclerView mRecyclerView;

    MainOptionItemRecAdapter mAdapter;

    private int screenWidth = 0;

    private int itemHeight = 0;


    private int mainPanelColumnNumber = Constants.MAIN_PANEL_ICON_COUNT;

    @Override
    public int getLayoutResId() {
        return R.layout.view_recycler_view;
    }

    @Override
    public void bindViews(View root) {
        mRecyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
        screenWidth = ViewUtils.getScreenWidth(IApplication.context());
        itemHeight = screenWidth / mainPanelColumnNumber;
    }

    @Override
    public void setViews() {

    }

    @Override
    public void handleData(MainRcvItemBase<List<MainPanelButton>> mainRcvItemBase, int position) {
        if (RcvViewType.MainRcvViewType.TYPE_MAIN_OPTION != mainRcvItemBase.getViewType()) {
            return;
        }
        final List<MainPanelButton> items   = mainRcvItemBase.getData();
        FullyGridLayoutManager      manager = new FullyGridLayoutManager(mRecyclerView.getContext(), mainPanelColumnNumber);
        mAdapter = new MainOptionItemRecAdapter(items);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
    }


    public class MainOptionItemRecAdapter extends LemonRcvAdapter<MainPanelButton> {


        public MainOptionItemRecAdapter(@Nullable List<MainPanelButton> data) {
            super(data);
        }


        @Override
        public int getItemCount() {
            return mainPanelColumnNumber * (getData().size()/3);
        }

        @NonNull
        @Override
        public AdapterItem createItem(Object type) {
            return new MainOptionItemRecItem();
        }

        public class MainOptionItemRecItem implements AdapterItem<MainPanelButton> {
            LinearLayout          ripple;
            BGABadgeFrameLayout layoutIcon;
            ImageView           iconImage;
            TextView            textView;


            private int mPosition;

            @Override
            public int getLayoutResId() {
                return R.layout.item_main_panel;
            }

            @Override
            public void bindViews(View root) {
                ripple = (LinearLayout) root.findViewById(R.id.ripple);
                layoutIcon = (BGABadgeFrameLayout) root.findViewById(R.id.layoutIcon);
                iconImage = (ImageView) root.findViewById(R.id.iconImage);
                textView = (TextView) root.findViewById(R.id.textView);
            }


            @Override
            public void setViews() {
                ripple.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MainPanelButtonEvent event = new MainPanelButtonEvent();
                        event.setTag(EventTag.TAG_MAIN_PANEL_BUTTON_EVENT);
                        event.setIndex(mPosition);
                        event.setButton(mDataList.get(mPosition));
                        AppService.getBus().post(event);
                    }
                });
         /*       ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                    @Override
                    public void onComplete(RippleView rippleView) {
                        MainPanelButtonEvent event = new MainPanelButtonEvent();
                        event.setTag(EventTag.TAG_MAIN_PANEL_BUTTON_EVENT);
                        event.setIndex(mPosition);
                        event.setButton(mDataList.get(mPosition));
                        AppService.getBus().post(event);
                    }
                });*/

            }

            @Override
            public void handleData(MainPanelButton data, int position) {
                mPosition = position;
                int iconHeight = ViewUtils.dip2px(IApplication.context(), 25) + getImageWH(data.getIconRes())[0];
                layoutIcon.setLayoutParams(new LinearLayout.LayoutParams(iconHeight, iconHeight));

                if(data.getBgRes()!=-1)
                layoutIcon.setBackgroundResource(data.getBgRes());

         /*       GradientDrawable myGrad = (GradientDrawable) layoutIcon.getBackground();
                myGrad.setColor(textView.getContext().getResources().getColor(data.getColorRes()));*/


                iconImage.setImageResource(data.getIconRes());
                textView.setText(textView.getContext().getText(data.getTitleRes()));

                if (data.getBadgeCount() <= 0) {
                    layoutIcon.hiddenBadge();
                } else {
                    layoutIcon.showTextBadge(data.getBadgeCount() + "");
                }
            }
        }
    }

    public int[] getImageWH(int Res) {
        Bitmap bitmap = BitmapFactory.decodeResource(IApplication.context().getResources(), Res);
        int    height = bitmap.getHeight();
        int    width  = bitmap.getWidth();
        bitmap = null;
        int[] result = {width, height};
        return result;
    }

}
